package com.painpointcloud.sparkshare;

import com.painpointcloud.blog.bean.Blog_user;
import com.painpointcloud.blog.service.UserService;
import com.painpointcloud.common.util.aimind.HtmlCompressor;
import com.painpointcloud.common.util.core.Page;
import com.painpointcloud.common.util.core.ResUtil;
import com.painpointcloud.common.util.core.ServiceException;
import com.painpointcloud.common.util.web.BaseController;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@Configuration
@ComponentScan(basePackages = "com")
@MapperScan("com.**.mapper")
@SpringBootApplication
public class SparkShareApplication extends BaseController{
	@Autowired
	private UserService service;
	@RequestMapping("/index.html")
	String indexMapping() {
    return "business/index";
	}
	@RequestMapping("/")
	String index() {
		return "business/index";
	}
	@RequestMapping("/coding.util")
	String autoCoding() {
		return "autoCoding/autoCoding";
	}

	/*@RequestMapping(value = "/user_view",method =RequestMethod.POST)
	@ResponseBody
	Map<String, Object> getUserList(@RequestBody Blog_user param, HttpServletRequest request) {
		try {
			setPageParam(param);
			List<Blog_user> list=service.selectUserList(param);
			Long count=(long)service.selectUserList(null).size();
			Page<Blog_user> page =getPageInfo(param,list,count);
			return ResUtil.Res(page);
		} catch (ServiceException se) {
			//log.warn("查询用户视图:"+se.getMessage());
			return ResUtil.Res(se);
		} catch (Exception e) {
			//log.error("查询用户视图:", e);
			return ResUtil.Res(e);
		}
	}*/
	@RequestMapping("/compress.business")
	@ResponseBody
	Map<String, Object> compressHtml(@RequestParam Map<String, Object> param, HttpServletRequest request) {
		Map<String, Object> returnMap=new HashMap<String, Object>();
		try {
			String html="";
			if(param.get("html")!=null)html=param.get("html").toString();
			String returnHtml= HtmlCompressor.compress(html);
			returnMap.put("returnSimpleHtml",returnHtml.replaceAll("\"","'"));
			returnMap.put("returnHtml",returnHtml);
			return ResUtil.Res(returnMap);//压缩html并把双引号替换为单引号
		} catch (ServiceException se) {
			//log.warn("查询用户视图:"+se.getMessage());
			return ResUtil.Res(se);
		} catch (Exception e) {
			//log.error("查询用户视图:", e);
			return ResUtil.Res(e);
		}
	}
//	    @Bean
//    public EmbeddedServletContainerFactory servletContainer() {
//      TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
//          @Override
//          protected void postProcessContext(Context context) {
//            SecurityConstraint securityConstraint = new SecurityConstraint();
//            securityConstraint.setUserConstraint("CONFIDENTIAL");
//            SecurityCollection collection = new SecurityCollection();
//            collection.addPattern("/*");
//            securityConstraint.addCollection(collection);
//            context.addConstraint(securityConstraint);
//          }
//        };
//
//      tomcat.addAdditionalTomcatConnectors(httpConnector());
//      return tomcat;
//    }
//    @Bean
//    public Connector httpConnector() {
//      Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
//      connector.setScheme("http");
//      connector.setPort(8080);
//      connector.setSecure(false);
//      connector.setRedirectPort(8443);
//      return connector;
//    }
	public static void main(String[] args) {
		SpringApplication app=new SpringApplication(SparkShareApplication.class);
		app.setBannerMode(Banner.Mode.OFF);
		app.run(args);
		//SpringApplication.run(SparkShareApplication.class, args);
	}

//	@Bean
//	public EmbeddedServletContainerCustomizer containerCustomizer() {
//
//		return (container -> {
//			ErrorPage error400Page = new ErrorPage(HttpStatus.BAD_REQUEST, "/error-400.html");
//			ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
//			ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
//			ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");
//			container.addErrorPages(error400Page,error401Page, error404Page, error500Page);
//		});
//	}
}
